package game.ui.core
{
	import away3d.containers.*;
	import com.greensock.*;
	import engine.asset.*;
	import engine.core.*;
	import engine.graphics.away3d.*;
	import engine.math.*;
	import engine.social.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import game.*;
	import game.actor.*;
	import game.graphics.*;
	import game.graphics.away3d.*;
	import game.messages.progression.*;
	import game.messages.quest.*;
	import game.messages.simulation.*;
	//import game.messages.viewer.*;
	import game.network.*;
	import game.quest.*;
	import game.ui.controls.*;
	import game.ui.events.*;
	import game.ui.hud.*;
	import game.ui.script.*;
	
	public class RxUI
	{
		public static const MINIMUM_STAGE_WIDTH:int = 760;
		public static const MINIMUM_STAGE_HEIGHT:int = 720;
		public static const QUEST_PORTRAIT_URL:String = "ui/portraits/";
		public static const NPC_COMBAT_LABEL_ATTACK:uint = 0;
		public static const NPC_COMBAT_LABEL_GOLD:uint = 1;
		public static const NPC_COMBAT_LABEL_INVENTORY_FULL:uint = 2;
		private static const TOOLTIP_OFFSET:int = 20;
		private static const TOOLTIP_EDGE_PADDING:int = 10;
		private static const HUD_TYPE_NORMAL:uint = 0;
		private static const HUD_TYPE_IMAGES:uint = 1;
		private static const NEW_GAME_TUTORIAL_FLAG:uint = 1;
		public static const TUTORIAL_FLAG_SKILLS:uint = 1 << 9;
		private static var s_hudType:int;
		private static var s_stage:Stage;
		private static var s_root:Sprite;
		private static var s_layers:Vector.<Sprite>;
		private static var s_updateQueue:Vector.<RxUIElement>;
		private static var s_eventDispatcher:EventDispatcher;
		private static var s_view3D:View3D;
		private static var s_hud:RxHud;
		private static var s_loadingScreen:RxLoadingScreen;
		private static var s_targetActor:RxActor;
		private static var s_equipmentPanel:RxEquipmentPanel;
		private static var s_skillsPanel:RxSkillsPanel;
		private static var s_friendsPanel:RxFriendsPanel;
		//private static var s_storePanel:RxStorePanel;
		//private static var s_mapPanel:RxMapPanel;
		private static var s_optionsMenu:RxOptionsMenu;
		private static var s_activePanel:RxGamePanel;
		private static var s_abilityCallback:Function;
		private static var s_abilityUpdateCallback:Function;
		private static var s_tooltipPosition:Point;
		//private static var s_itemTooltip:RxEquipmentTooltip;
		private static var s_notifications:Vector.<RxUIElement>;
		private static var s_tutorialsCompleted:uint;
		private static var s_uiFlags:uint;
		private static var s_tutorialsInProgress:uint;
		private static var s_unlockedMapIndices:Vector.<uint>;
		private static var s_selectedHudImage:int = -1;
		private static var s_hudBitmap:Sprite;
		//private static var s_hudImageAssets:RxAssetGroup;
		private static var s_mouseEventResponders:Array;
		private static var s_spotlightSizes:Array;
		private static var s_spotlightOffset:Point;
		private static var s_spotlightVisible:Boolean = false;
		private static var s_gameLoading:Boolean = true;
		
		public function RxUI()
		{
		}
		
		public static function AddElementToLayer(element:RxUIElement, layerLevel:int) : void
		{
			if (layerLevel < 0)
			{
				layerLevel = 0;
			}
			
			if (layerLevel > RxUILayerConstants.MAX_LAYERS)
			{
				layerLevel = RxUILayerConstants.MAX_LAYERS - 1;
			}
			
			var layer:int = element.GetLayer();
			if (layer != RxUILayerConstants.NO_LAYER)
			{
				s_layers[layer].removeChild(element);
			}
			
			s_layers[layerLevel].addChild(element);
			element.SetLayer(layerLevel);
		}
		
		public static function ToggleMouseShield(IsBuild:Boolean, layer:int = -1) : void
		{
			if (layer == -1)
			{
				layer = RxUILayerConstants.MODAL_DIALOGS;
			}
			
			var sp:Sprite = GetUILayer(layer);
			sp.graphics.clear();
			
			if (IsBuild)
			{
				sp.graphics.beginFill(0, 0.6);
				sp.graphics.drawRect(0, 0, s_stage.stageWidth, s_stage.stageHeight);
				sp.graphics.endFill();
			}
		}
		
		public static function GetUILayer(layer:int) : Sprite
		{
			return s_layers[layer];
		}
		
		public static function RemoveElementFromUpdateQueue(element:RxUIElement) : void
		{
			var index:int = s_updateQueue.indexOf(element);
			if (index != -1)
			{
				var popElement:RxUIElement = s_updateQueue.pop();
				
				if (popElement != element)
				{
					s_updateQueue[index] = popElement;
				}
			}
		}
		
		public static function RemoveElementFromLayer(element:RxUIElement) : void
		{
			var layer:int = element.GetLayer();
			if (layer == RxUILayerConstants.NO_LAYER)
			{
				return;
			}
			
			element.SetLayer(RxUILayerConstants.NO_LAYER);
			
			if (element.stage)
			{
				s_layers[layer].removeChild(element);
			}
		}
		
		public static function SetAbilityUpdateCallback(callback:Function) : void
		{
			s_abilityUpdateCallback = callback;
		}
		
		public static function Initialize(param1:Stage, param2:View3D) : void
		{
			var _loc_4:Sprite = null;
			s_stage = param1;
			s_eventDispatcher = new EventDispatcher();
			s_view3D = param2;
			s_root = new Sprite();
			s_root.mouseEnabled = false;
			s_stage.addChild(s_root);
			s_layers = new Vector.<Sprite>(RxUILayerConstants.MAX_LAYERS, true);
			var _loc_3:int = 0;
			while (_loc_3 < RxUILayerConstants.MAX_LAYERS)
			{
				
				_loc_4 = new Sprite();
				s_layers[_loc_3] = _loc_4;
				s_root.addChild(_loc_4);
				_loc_3++;
			}
			s_layers[RxUILayerConstants.GAME].mouseChildren = false;
			s_layers[RxUILayerConstants.GAME].mouseEnabled = false;
			s_layers[RxUILayerConstants.DRAG].mouseEnabled = false;
			s_layers[RxUILayerConstants.TOOLTIP].mouseEnabled = false;
			s_layers[RxUILayerConstants.HUD].mouseEnabled = false;
			s_updateQueue = new Vector.<RxUIElement>;
			new RxDialogManager();
			AddTransactionServerListeners();
		}
		
		public static function AddTransactionServerListeners() : void
		{
			//RxTransactionServer.instance.AddListener(RxProgression_RecordStateMessage.messageId, OnTutorialsClearedMessageReceived);
			//RxTransactionServer.instance.AddListener(RxProgression_MapsUnlockedMessage.messageId, OnMapsUnlockedMessage);
		}
		
		public static function ShowLoadingScreen(param1:Boolean = false) : void
		{
			if (s_loadingScreen)
			{
				return;
			}
			s_loadingScreen = new RxLoadingScreen();
			AddElementToLayer(s_loadingScreen, RxUILayerConstants.TOP);
			if (param1)
			{
				s_loadingScreen.alpha = 0;
				TweenLite.to(s_loadingScreen, 0.2, {alpha:1});
			}
			HideGamePanel();
		}
		
		public static function OnLocaleChanged() : void
		{
		}
		
		public static function HideGamePanel(param1:int = -1) : void
		{
			s_view3D.hitField.removeEventListener(MouseEvent.MOUSE_DOWN, OnStageMouseDown);
			if (s_activePanel && s_activePanel.CanHide())
			{
				RemoveElementFromLayer(s_activePanel);
				s_activePanel = null;
				if (s_hud)
				{
					s_hud.OnGamePanelToggled();
				}
			}
		}
		
		public static function OnStageMouseDown(event:MouseEvent) : void
		{
			HideGamePanel();
		}
		
		public static function OnChangingMap() : void
		{
			s_gameLoading = true;
			ShowLoadingScreen(true);
			RxUIScriptRunner.EndAll();
			RxDialogManager.instance.HideAllDialogs();
			if (s_hud)
			{
				s_hud.OnChangingMap();
			}
			//ClearSpotlight();
			//HideAllTooltips();
		}
		
		public static function OnGameLoadComplete() : void
		{
			/*
			if (s_hud)
			{
				s_hud.OnMapLoaded();
			}
			HideLoadingScreen();
			if (RxUI.GetUIPropertyValue(RxProgression_SetUIPropertyMessage.WELCOME_SCREEN_SHOWN))
			{
				CheckNewGameTutorialStatus();
			}
			else
			{
				ShowWelcomeScreen();
			}
			s_gameLoading = false;
			if (s_eventDispatcher.hasEventListener(RxUIEvent.MAP_CHANGE_COMPLETE))
			{
				s_eventDispatcher.dispatchEvent(new RxUIEvent(RxUIEvent.MAP_CHANGE_COMPLETE));
			}*/
		}
		
		public static function UpdateAll(param1:Number) : void
		{
			/*
			var _loc_2:RxUIElement = null;
			for each (_loc_2 in s_updateQueue)
			{
				
				_loc_2.Update(param1);
			}
			if (s_itemTooltip && !s_tooltipPosition)
			{
				PositionItemTooltip();
			}
			if (RxKeyboard.isPressed(RxKeyboard.KEY_SPACE))
			{
				HideGamePanel();
				if (s_hud)
				{
					s_hud.ToggleActionBarAssignmentPanel(false);
				}
			}
			if (s_mouseEventResponders)
			{
				DrawSpotlight();
			}
			return;*/
		}
		
		public static function GetAssetPreloader() : RxAssetGroup
		{
			var _local1:XML = <assetGroup>
				<!-- HUD -->
				<image url="ui/hud2/hud_frame.png"/>
				<image url="ui/hud2/health.png"/>
				<image url="ui/hud2/energy.png"/>
				<image url="ui/hud2/hud_frame.png"/>
				<image url="ui/hud2/nav_bg.png"/>
				<image url="ui/hud2/nav_slot_bg.png"/>
				<image url="ui/hud2/swap_frame.png"/>
				<image url="ui/hud2/xp_bar.png"/>
				<image url="ui/hud2/health_enemy.png"/>
				<image url="ui/icons/checkmark.png"/>
				<image url="ui/hud/counters/counter_bg.png"/>
				<image url="ui/icons/throbber.png"/>
				
				<image url="ui/notifications/bark1.png"/>
				<image url="ui/notifications/barktail.png"/>
				<image url="ui/notifications/ragged1.png"/>
				
				<!-- RxGamePanel -->
				<image url="ui/roboframe_a/base_s9.png"/>
				<image url="ui/roboframe_a/body_s9.png"/>
				<image url="ui/roboframe_a/stack_s9.png"/>
				<image url="ui/roboframe_a/tile_grime2.png"/>
				<image url="ui/roboframe_a/tile_headlineborder.png"/>
				<image url="ui/roboframe_a/well_s9.png"/>
				<image url="ui/inventory/scroll_arrow.png"/>

				<!-- RxRectangularButton -->
				<image url="ui/skills/softgap_s9.png"/>
				<image url="ui/btn_standard/btn_standard_s9.png"/>
				
				<!-- RxQuestMessageDialog -->
				<image url="ui/questdialog/portraitframe.png"/>
				<image url="ui/questdialog/portraitframe_namewrap.png"/>
				<image url="ui/questdialog/button_cradle.png"/>
				
				<!-- RxEquipmentPanel -->
				<image url="ui/icons/icon_attackrating2_sm.png"/>
				<image url="ui/icons/icon_armorrating_sm.png"/>
				<image url="ui/questpanel/slot_inner_s9.png"/>
				<image url="ui/roboframe_a/tab_active_s9.png"/>
				<image url="ui/roboframe_a/tab_inactive_s9.png"/>
				
				<!-- RxStorePanel -->
				<image url="ui/shop/paneltx.png"/>
				<image url="ui/shop/listitem_wrapper_s9.png"/>
				<image url="ui/shop/listitem_overlay_s9.png"/>
				<image url="ui/shop/counterbkg_s9.png"/>
				<image url="ui/icons/banner2_lg.png"/>

				<!-- RxQuestTracker -->
				<image url="ui/questtracker/divider.png"/>
				<image url="ui/questtracker/top_frame.png"/>
				<image url="ui/questtracker/bottom_frame.png"/> 
				<image url="ui/questtracker/tracker_icon.png"/>
				<image url="ui/questtracker/close_arrow.png"/>
				<image url="ui/questtracker/ribbon.png"/>
				<image url="ui/hud/wireframe/lock_icon.png"/>
				<image url="ui/roboframe_a/mellowdivider.png"/>
				
				<!-- RxMap -->
				<image url="ui/worldmap/star.png"/>
				
				<!-- RxSkillPanel -->
				<image url="ui/skills/skillpoint_counter_sm.png"/>
				<image url="ui/hud2/skill_bg.png"/>
				<image url="ui/hud2/ability_ready.png"/>
				<image url="ui/hud2/skill_empty.png"/>
				<image url="ui/hud2/ability_gate_left.png"/>
				<image url="ui/hud2/ability_gate_right.png"/>
				<image url="ui/hud2/consumable_bg.png"/>
			</assetGroup>;
			var _local2:RxAssetGroup = new RxAssetGroup();
			_local2.Init(_local1, null);
			return _local2;
		}
		
		public static function CreateHUD() : void
		{
			var _loc_1:RxAssetXML = null;
			if (RxClientVars.GetInt("disableHud"))
			{
				return;
			}
			s_hudType = RxClientVars.GetInt("hudType");
			if (s_hudType == HUD_TYPE_NORMAL)
			{
				if (s_hud == null)
				{
					s_hud = new RxHud();
					AddElementToLayer(s_hud, RxUILayerConstants.HUD);
				}
			}
			else
			{
				_loc_1 = new RxAssetXML();
				_loc_1.SetURL("hudImages.xml");
				//_loc_1.AddCallback(new RxAssetCallback(OnHudXMLFileLoaded));
				_loc_1.Load();
			}
			s_stage.addEventListener(Event.RESIZE, OnStageResized, false, 0, true);
		}
		
		public static function OnStageResized(event:Event) : void
		{
			PositionHUDItems();
			if (s_mouseEventResponders)
			{
				//DrawSpotlight();
			}
		}
		
		public static function PositionHUDItems(event:Event = null) : void
		{
			var _loc_2:RxButtonBase = null;
			var _loc_3:Point = null;
			var _loc_4:int = 0;
			if (s_activePanel)
			{
				if (s_activePanel == s_skillsPanel)
				{
					s_activePanel.x = Math.floor((s_stage.stageWidth - s_activePanel.width) * 0.5);
					s_activePanel.y = Math.floor((s_stage.stageHeight - 100 - s_activePanel.height) * 0.5);
				}
				else if (s_activePanel == s_optionsMenu)
				{
					if (s_hud)
					{
						//_loc_2 = s_hud.GetOptionsButton();
						_loc_3 = _loc_2.localToGlobal(new Point(0, _loc_2.height));
						s_activePanel.x = _loc_3.x + 2;
						s_activePanel.y = _loc_3.y;
					}
				}
				else if (s_activePanel != s_friendsPanel)
				{
					s_activePanel.x = Math.floor((s_stage.stageWidth - s_activePanel.width) * 0.5);
					s_activePanel.y = Math.floor((s_stage.stageHeight - s_activePanel.height) * 0.5);
				}
				else
				{
					_loc_4 = (s_stage.stageWidth - 760) * 0.5;
					//s_friendsPanel.x = _loc_4 + 760 - s_friendsPanel.width;
					//s_friendsPanel.y = s_stage.stageHeight - s_friendsPanel.height - 160;
				}
			}
		}
		
		public static function PostLoadAssets() : void
		{
			var _local1:XML = <assetGroup>
					<image url="ui/worldmap/friend_pointer.png"/>
					<image url="items/bundle_gold/bundle_gold1_lg.png" name="GoldLarge1"/>
					<image url="items/bundle_gold/bundle_gold2_lg.png" name="GoldLarge2"/>
					<image url="items/bundle_gold/bundle_gold3_lg.png" name="GoldLarge3"/>
					<image url="ui/worldmap/v2/world_map.jpg"/>
				</assetGroup>;
			var _local2:RxAssetGroup = new RxAssetGroup();
			_local2.Init(_local1, null);
			_local2.Load();
		}
		
		public static function AddElementToUpdateQueue(param1:RxUIElement) : void
		{
			if (s_updateQueue.indexOf(param1) == -1)
			{
				s_updateQueue.push(param1);
			}
		}
	}
}